package cn.mugutu.finance.task;

import cn.mugutu.finance.domain.DoubleBall;
import cn.mugutu.finance.enums.DoubleBallNumberType;
import cn.mugutu.finance.service.DoubleBallService;
import cn.mugutu.finance.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 双色球task  https://datachart.500.com/ssq/history/history.shtml
 *
 * @author gan
 * @date 2018/12/12 2:22 PM
 */

@Component
@Slf4j
public class AwardTask {

    @Autowired
    private DoubleBallService doubleBallService;

    //@Scheduled(cron = "0/5 * *  * * ? ") //每隔1秒隔行一次
    @Scheduled(cron = "0 0 12 * * ?") //每天中午12点
    public void run() {
        try {

            log.info("定时任务开始拉取数据....");
            //查询数据库中最大一期的期数
            Integer startPeriod = doubleBallService.getMaxPeriod();
            if (startPeriod == null) {
                startPeriod = 1;
            }

            Integer endPeriod = getmaxPeriod();
            log.info("目前系统保存最大一期为=========>" + startPeriod);
            log.info("实际最近一期为================>" + endPeriod);

            if (endPeriod.equals(startPeriod)) {
                return;
            }

            //查询目前最大的一期期数
            List<DoubleBall> list = getDoubleBall(startPeriod, endPeriod);
            if (!CollectionUtils.isEmpty(list)) {
                for (DoubleBall d : list) {
                    doubleBallService.save(d);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.info("抓取数据失败.....");
        }

    }

    /**
     * 查询目前最大的一期期数
     *
     * @return
     * @throws IOException
     */
    public static Integer getmaxPeriod() throws IOException {
        Connection con = Jsoup.connect("https://datachart.500.com/ssq/history/newinc/history.php?limit=1&sort=0");
        Document doc = con.get();
        Element tdata = doc.getElementById("tdata");
        Elements td = tdata.getElementsByTag("td");
        String text = td.first().text();
        return Integer.parseInt(text);
    }

    /**
     * 查询指定范围内期数双色球
     *
     * @param start
     * @param end
     * @return
     * @throws IOException
     */
    public static List<DoubleBall> getDoubleBall(Integer start, Integer end) throws Exception {
        Connection con = Jsoup.connect("https://datachart.500.com/ssq/history/newinc/history.php?start=" + start + "&end=" + end);
        Document doc = con.get();
        Element tdata = doc.getElementById("tdata");
        Elements trs = tdata.getElementsByTag("tr");

        List<DoubleBall> list = new ArrayList<>();
        DoubleBall doubleBall;
        Iterator<Element> iterator = trs.iterator();
        while (iterator.hasNext()) {
            doubleBall = new DoubleBall();
            Element next = iterator.next();
            Elements tds = next.getElementsByTag("td");
            doubleBall.setPeriod(Integer.parseInt(tds.get(0).text()));
            doubleBall.setRed1(Integer.parseInt(tds.get(1).text()));
            doubleBall.setRed2(Integer.parseInt(tds.get(2).text()));
            doubleBall.setRed3(Integer.parseInt(tds.get(3).text()));
            doubleBall.setRed4(Integer.parseInt(tds.get(4).text()));
            doubleBall.setRed5(Integer.parseInt(tds.get(5).text()));
            doubleBall.setRed6(Integer.parseInt(tds.get(6).text()));
            doubleBall.setBlueNumber(Integer.parseInt(tds.get(7).text()));
            doubleBall.setAwardPoll(formatNumber(tds.get(9).text()));
            doubleBall.setOneNumber(Integer.parseInt(tds.get(10).text()));
            doubleBall.setOneAwardMoney(formatNumber(tds.get(11).text()));
            doubleBall.setTwoNumber(Integer.parseInt(tds.get(12).text()));
            doubleBall.setTwoAwardMoney(formatNumber(tds.get(13).text()));
            doubleBall.setTotalMoney(formatNumber(tds.get(14).text()));
            doubleBall.setOpenAwardDate(DateUtil.parseDate(tds.get(15).text()));
            doubleBall.setType(DoubleBallNumberType.ACTUAL);    //真实数据
            list.add(doubleBall);
        }
        return list;
    }

    public static int formatNumber(String old) throws ParseException {
        return NumberFormat.getInstance().parse(old).intValue();
    }


}
